package cn.com.jenson.service.main.service.impl;

import cn.com.jenson.common.ImportVerifierVo;
import cn.com.jenson.common.SelectVo;
import cn.com.jenson.entity.main.Log;
import cn.com.jenson.entity.main.QLog;
import cn.com.jenson.exception.BusinessException;
import cn.com.jenson.service.main.repository.LogRepository;
import cn.com.jenson.service.main.service.LogService;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class LogServiceImpl implements LogService {

    @Resource
    LogRepository logRepository;

    @Resource
    JPAQueryFactory jpaQueryFactory;

    QLog qLog = QLog.log;

    @Override
    public Page<Log> pageAll(Log log) {

        JPAQuery<Log> jpaQuery = jpaQueryFactory
                .selectFrom(qLog)
                .where(getWhere(log));

        List<Log> list = jpaQuery
                .offset(log.getPageRequest().getOffset())
                .limit(log.getPageRequest().getPageSize())
                .fetch();

        supplement(list);

        return new PageImpl<>(
                list,
                log.getPageRequest(),
                jpaQuery.fetchCount()
        );
    }

    @Override
    public List<Log> listAll(Log log) {
        List<Log> list = jpaQueryFactory
                .selectFrom(qLog)
                .where(getWhere(log))
                .fetch();

        supplement(list);
        return list;
    }

    @Override
    public Log get(Log log) throws InstantiationException, IllegalAccessException {
        Log entity = logRepository.findByIdAndDeleted(log.getId(), false)
                .orElseThrow(() -> new BusinessException("找不到id为" + log.getId() + "的购买记录"));
        supplement(entity);
        return entity;
    }

    @Override
    public BooleanExpression getWhere(Log log) {
        return null;
    }

    @Override
    public Log edit(Log log) throws InstantiationException, IllegalAccessException {
        logRepository.save(log);
        return log;
    }

    @Override
    public List<Log> editAll(List<Log> logs) {
        return null;
    }

    @Override
    public Log deleted(Log log) throws InstantiationException, IllegalAccessException {
        Log entity = logRepository.findByIdAndDeleted(log.getId(), false)
                .orElseThrow(() -> new BusinessException("找不到id为" + log.getId() + "的购买记录"));
        entity.setDeleted(true);
        logRepository.save(entity);
        return entity;
    }

    @Override
    public List<Log> deletedAll(List<Log> logs) {
        return null;
    }

    @Override
    public List<ImportVerifierVo> importTemplateVerifiers() {
        return null;
    }

    @Override
    public void importTemplateDownload(HttpServletResponse response) throws IOException {

    }

    @Override
    public List<SelectVo<String, Integer, Log>> option() {
        return null;
    }

    @Override
    public void supplement(Log log) {

    }

    @Override
    public void supplement(List<Log> logs) {

    }

    @Override
    public void supplement(Page<Log> logs) {

    }
}
